
#include "teacherwidget.h"
#include "ui_teacherwidget.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QTableWidgetItem>

#include "TeacherMyStudentInfo.h"
#include "studentsql.h"

teacherwidget::teacherwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::teacherwidget)
{
    ui->setupUi(this);
    //进行信号连接
    connect(ui->pushButton_3,&QPushButton::clicked,this,&teacherwidget::on_pushButton_3_clicked);



    //查看支持的数据库版本
//    qDebug() << QSqlDatabase::drivers() << endl;

//    db = QSqlDatabase::addDatabase("QMYSQL");//添加mysql
//    db.setHostName("192.168.175.131");
//    db.setUserName("root");
//    db.setPassword("123456");
//    db.setDatabaseName("test268");
//    db.setPort(3306);
//    if(!db.open())
//    {
//        qDebug() << db.lastError() << endl;
//        return ;
//    }
    QSqlQuery query(db);//mysql语句执行对象

    if(query.exec("select *from student_list;") == false)
    {
        qDebug() << db.lastError() << endl;
        return;
    }



    QStringList headers;
    headers<<"班级" << "学号" << "姓名" << "性别"<<"老师";
    //设置列数
    ui->tableWidget->setColumnCount(5);
    //HorizontalHeaderLabels(水平方向)
    ui->tableWidget->setHorizontalHeaderLabels(headers);

    //设置在第几行插入数据
    ui->tableWidget->setRowCount(0);
    int currentRow = 0;

    while( query.next() == true) //从结果集中读取下一行数据
    {
        QTableWidgetItem *ClassItem = new QTableWidgetItem(query.value("class").toString());
        QTableWidgetItem *IDItem = new QTableWidgetItem(query.value("id").toString());
        QTableWidgetItem *NameItem = new QTableWidgetItem(query.value("Name").toString());
        QTableWidgetItem *SexItem = new QTableWidgetItem(query.value("Sex").toString());
        QTableWidgetItem *TeacherItem = new QTableWidgetItem(query.value("teacher").toString());
        ui->tableWidget->insertRow(currentRow);
        ui->tableWidget->setItem(currentRow, 0, ClassItem);
        ui->tableWidget->setItem(currentRow, 1, IDItem);
        ui->tableWidget->setItem(currentRow, 2, NameItem);
        ui->tableWidget->setItem(currentRow, 3, SexItem);
        ui->tableWidget->setItem(currentRow, 4, TeacherItem);
        currentRow++;

    }

}

teacherwidget::~teacherwidget()
{
    delete ui;
}

void teacherwidget::on_pushButton_clicked()
{
    exit(0);
}

void teacherwidget::on_pushButton_2_clicked()
{
    SearchData();
}

void teacherwidget::SearchData()
{
    //获取用户输入的查询条件
    QString text=ui->lineEdit->text();
    //清空现有的数据
    ui->tableWidget->setRowCount(0);
    //判断用户是否输入查询
    if(text.isEmpty())
    {
        qDebug()<<"没有查询!";
        return ;
    }
    //使用用户输入的查询条件构建SQL查询语句
    //模糊查询
    QString queryStr = QString("select * from student_list where Name LIKE '%%1%'").arg(text);

    qDebug()<<queryStr;
    //执行查询
    QSqlQuery query(db);
    if(!query.exec(queryStr))
    {
        qDebug()<<"查询失败:"<<query.lastError();
        return ;
    }
    if(!query.next())
    {
        qDebug()<<"没有找到数据!";
    }

    //查询成功,更新表格
    int currentRow = 0;
    //这边需要先运行在进行判断，否则会判断query.next为空
   do
    {
//        qDebug()<<queryStr;
        QTableWidgetItem *ClassItem = new QTableWidgetItem(query.value("class").toString());
        QTableWidgetItem *IDItem = new QTableWidgetItem(query.value("id").toString());
        QTableWidgetItem *NameItem = new QTableWidgetItem(query.value("name").toString());
        QTableWidgetItem *SexItem = new QTableWidgetItem(query.value("sex").toString());
        QTableWidgetItem *TeacherItem = new QTableWidgetItem(query.value("teacher").toString());

        ui->tableWidget->insertRow(currentRow);
        ui->tableWidget->setItem(currentRow, 0, ClassItem);
        ui->tableWidget->setItem(currentRow, 1, IDItem);
        ui->tableWidget->setItem(currentRow, 2, NameItem);
        ui->tableWidget->setItem(currentRow, 3, SexItem);
        ui->tableWidget->setItem(currentRow, 4, TeacherItem);
        currentRow++;
    } while(query.next());

    qDebug()<<"查询结束!";
}


void teacherwidget::on_pushButton_3_clicked()
{
    TeacherMyStudentInfo *TeaInfo=new TeacherMyStudentInfo;
    TeaInfo->show();
}
